﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SpaceGame.Managers;

namespace SpaceGame.Scenes
{
    public class MainMenuScene : DrawableGameComponent
    {
        private SpriteBatch m_SpriteBatch;
        private SpriteFont m_SpriteFont;
        private KeyboardState m_LastKeyboardState;
        private int m_SelectedIndex = 0;
        private List<string> m_Items;

        public int SelectedIndex
        {
            get { return m_SelectedIndex; }
            set 
            {
                if (value > m_Items.Count - 1)
                    value = m_Items.Count - 1;

                if (value < 0)
                    value = 0;

                m_SelectedIndex = value; 
            }
        }

        public MainMenuScene(Game game) : base(game)
        {
            m_SpriteBatch = new SpriteBatch(game.GraphicsDevice);
            m_Items = new List<string>();
            m_Items.Add("Oyuna Başla");
            m_Items.Add("En İyi Skorlar");
            m_Items.Add("Ayarlar");
            m_Items.Add("Çıkış");
        }

        protected override void LoadContent()
        {
            m_SpriteFont = Game.Content.Load<SpriteFont>("Fonts/Tahoma");
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            KeyboardState keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyUp(Keys.Up) && m_LastKeyboardState.IsKeyDown(Keys.Up))
                SelectedIndex--;

            if (keyboardState.IsKeyUp(Keys.Down) && m_LastKeyboardState.IsKeyDown(Keys.Down))
                SelectedIndex++;

            if (keyboardState.IsKeyUp(Keys.Enter) && m_LastKeyboardState.IsKeyDown(Keys.Enter))
            {
                if (SelectedIndex == 0)
                {
                    Enabled = false;
                    Visible = false;
                    GameStateManager.GameState = GameState.Playing;
                }
                else if (SelectedIndex == 3)
                    Game.Exit();
            }

            m_LastKeyboardState = Keyboard.GetState();
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Color c;
            m_SpriteBatch.Begin();
            int drawY = 0;
            float scale = 1f;
            for (int i = 0; i < m_Items.Count; i++)
            {
                if (m_SelectedIndex == i)
                {
                    c = Color.Yellow;
                    scale = 1.4f;
                }
                else
                {
                    c = Color.White;
                    scale = 1f;
                }
                drawY += 50;
                m_SpriteBatch.DrawString(m_SpriteFont, m_Items[i], new Vector2(15, drawY), c, 0f, Vector2.Zero,
                    scale, SpriteEffects.None, 1f);
            }
            m_SpriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
